<?php

namespace app\api\controller\web\frontend;

use app\common\controller\Api;
use think\facade\Db;


/**
 * 荣誉展厅
 */
class Honor extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public $address = [
        '顺德园','禅城园','南海园','高明园','三水园'
    ];

    public $address_two = [
        '顺德区','禅城区','南海区','高明区','三水区'
    ];
    
    /**
     * 专利奖列表
     */
    public function list()
    {

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;

        $years = input('years') ?? '';
        $address = input('address') ?? '';
        $prize = input('prize') ?? '';
        $name = input('name') ?? '';

        $where = [];
        if ($years) {
            $where[] = ['years', '=', $years];
        }

        if ($address) {
            $where[] = ['address', '=', $address];
        }

        if ($prize) {
            $where[] = ['state', '=', $prize];
        }

        if ($name) {
            $where[] = ['name', 'LIKE', '%'.$name.'%'];
        }

        $list = Db::name('web_honor_patent')->where($where)->limit($start, $page_size)->order('years desc,state asc')->select()->toArray();
        foreach ($list as $k => $v) {
            switch ($v['state']) {
                case 1:
                    $list[$k]['prize'] = '金奖';
                    break;

                case 2:
                    $list[$k]['prize'] = '银奖';
                    break;
                
                default:
                    $list[$k]['prize'] = '优秀奖';
                    break;
            }
        }


        $count = Db::name('web_honor_patent')->where($where)->count();

        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);

    }

    // 搜索框的条件
    // type==0 默认专利奖的 1为示范区的
    // 顺德区 禅城区 南海区 高明区 三水区
    public function honor_serch_info()
    {
        $type = input('type') ?? 0;
        if ($type==0) {
            $address = $this->address_two;
            $years = Db::name('web_honor_patent')
                        ->field('years')
                        ->group('years')
                        ->order('years desc')
                        ->select()->toArray();
            $years = array_column($years,'years');
            $data = [
                'address'=>$address,
                'years'=>$years,
            ];
            $this->success('success',$data);
        }
        if ($type==1) {
            $address = $this->address_two;
            $years = Db::name('web_honor_examples')
                        ->field('years')
                        ->group('years')
                        ->order('years desc')
                        ->select()->toArray();
            $years = array_column($years,'years');
            $data = [
                'address'=>$address,
                'years'=>$years,
            ];
            $this->success('success',$data);
        }
    }

    // 统计五个区的专利奖和示范奖的数量
    public function get_address_num()
    {
        $years = input('years') ?? '';
        $address = $this->address_two;
        $data = [];
        if (!empty($address)) {
            foreach ($address as $k => $v) {
                $num1 = Db::name('web_honor_patent')->where('address',$v)->count();
                $arr = Db::name('web_honor_examples')
                        ->field('count(*) as num,type_id')
                        ->where('address',$v)->group('type_id')
                        ->select()->toArray();
                $result = array_column($arr,'num','type_id');
                $data[$k]['id'] = $k+1;
                $data[$k]['address'] = $v;
                $data[$k]['num1'] = $num1;
                $data[$k]['num2'] = isset($result[8])?$result[8]:0;
                $data[$k]['num3'] = isset($result[9])?$result[9]:0;
                $data[$k]['num4'] = isset($result[10])?$result[10]:0;
                $data[$k]['num5'] = isset($result[11])?$result[11]:0; //专利奖需要自己统计就是num1 这个不需要
                $data[$k]['num6'] = isset($result[12])?$result[12]:0;
            }
        }
        $this->success('success',$data);
    }

    // 园区荣誉的饼图和柱状图
    // 12湾高赛
    public function honor_chart()
    {
        $type = input('type') ?? 12;

        $address = $this->address_two;
        $data = [];
        $cake1 = [];
        $cake2 = [];
        $columnar1 = [];
        $columnar2 = [];
        if (!empty($address)) {
            foreach ($address as $k => $v) {
                $num1 = Db::name('web_honor_patent')->where('address',$v)->count();
                $cake1[$k]['value'] = $num1;
                $cake1[$k]['name'] = $v;
                $columnar1[$k] = $num1;

                $num2 = Db::name('web_honor_examples')
                        ->where('address',$v)
                        ->where('type_id',$type)
                        ->count();
                $cake2[$k]['value'] = $num2;
                $cake2[$k]['name'] = $v;
                $columnar2[$k] = $num2;
            }
        }
        $data = [
            'cake_patent'=>$cake1,
            'cake_wan'=>$cake2,
            'columnar_patent'=>$columnar1,
            'columnar_wan'=>$columnar2,
        ];
        $this->success('success',$data);
    }



    /**
     * 各奖项数
     */
    public function get_prize_num()
    {
        $data = [];
        // 金奖
        $data[] = Db::name('web_honor_patent')->where('prize', 1)->count();

        // 银奖
        $data[] = Db::name('web_honor_patent')->where('prize', 2)->count();

        // 优秀奖
        $data[] = Db::name('web_honor_patent')->where('prize', 3)->count();

        return $data;

    }

    /**
     * 获取示范企业
     */
    public function get_examples()
    {

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;

        $type = input('type') ?? '';
        $years = input('years') ?? '';
        $address = input('address') ?? '';
        $name = input('name') ?? '';

        $where = [];

        if ($type) {
            $where[] = ['type_id', '=', $type];
        }

        if ($years) {
            $where[] = ['years', '=', $years];
        }

        if ($address) {
            $where[] = ['address', '=', $address];
        }

        if ($name) {
            $where[] = ['name', 'LIKE', '%'.$name.'%'];
        }
        $arr = Db::name('web_label')->where('id','>',0)->select()->toArray();
        $type_arr = array_column($arr, 'name','id');

        $list = Db::name('web_honor_examples')->where($where)->limit($start, $page_size)->select()->toArray();
        foreach ($list as $k => $v) {
            $list[$k]['type_name'] = $type_arr[$v['type_id']];
        }

        $count = Db::name('web_honor_examples')->where($where)->count();

        $data = [
            'page'=>$page,
            'page_size'=>$page_size,
            'count'=>$count,
            'list'=>$list,
        ]; 

        $this->success('success',$data);
    }


    /**
     * 商标
     */
    public function get_trademark()
    {

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;

        $years = input('years') ?? '';
        $address = input('address') ?? '';

        $where = [];
        if ($years) {
            $where[] = ['years', '=', $years];
        }

        if ($address) {
            $where[] = ['address', '=', $address];
        }

        $list = Db::name('web_honor_trademark')->where($where)->limit($start, $page_size)->select()->toArray();


        $count = Db::name('web_honor_trademark')->where($where)->count();

        return ajaxReturn('success', 1, [
            'page' => $page,
            'page_size' => $page_size,
            'count' => $count,
            'list' => $list,
        ]);

        // $list = Db::name('web_honor_trademark')
        //                 ->field('id, name, tpic, href')
        //                 ->order('myorder desc, create_time desc')
        //                 ->select()
        //                 ->toArray();
        // return $list;
    }


    /**
     * 地理标志
     */
    public function get_landmark()
    {
        $list = Db::name('web_honor_landmark')
                        ->field('id, name, tpic')
                        ->order('myorder desc, create_time desc')
                        ->select()
                        ->toArray();
        return $list;
    }


    /**
     * 对话顺德
     */
    public function get_dialogue()
    {

        // 分页
        $page = !empty(input('page')) ? input('page') : 1;
        $page_size = !empty(input('page_size')) ? input('page_size') : 10;
        if ($page < 1) {
            $page = 1;
        }
        $start = ($page - 1) * $page_size;

        $list = Db::name('web_honor_dialogue')
                        ->field('id, title, tpic, content')
                        ->order('myorder desc, online_time desc')
                        ->limit($start, $page_size)
                        ->select()
                        ->toArray();

        foreach ($list as $k => $v) {
            $list[$k]['content'] = strip_tags(htmlspecialchars_decode($v['content']));
        }

        $count = Db::name('web_honor_dialogue')->count();

        return ajaxReturn('success', 1, [
            'page' => $page,
            'page_size' => $page_size,
            'count' => $count,
            'list' => $list,
        ]);

        return $list;
    }



    /**
     * 指定搜索三合一
     */
    public function get_search()
    {   
        $table = input('table') ?? '';
        $type = input('type') ?? '';
        $address = input('address') ?? '';
        $prize_name = input('prize_name') ?? '';
        $years = input('years') ?? '';

        if (empty($table)) {
            return ajaxReturn('table不能为空', 0);
        }

        $where = [];

        if ($table == 'examples') {
            if (empty($type)) {
                return ajaxReturn('示范企业类型未选择', 0);
            }

            $where[] = ['type', '=', $type];
        }

        if ($years) {
            $where[] = ['years', '=', $years];
        }
        
        if ($address) {
            $where[] = ['address', '=', $address];
        }

        if ($prize_name) {
            $where[] = ['prize_name', '=', $prize_name];
        }

        $data['years'] = Db::name('web_honor_'.$table)->where($where)->group('years')->order('years asc')->column('years');

        // 街镇 带排序
        $sql = Db::name('web_honor_'.$table)->field('address')->where($where)->group('address')->buildSql();
        $sql .= ' ORDER BY CASE address WHEN address = "其它" THEN 0 END DESC';
        $data['address'] = Db::query($sql);
        $data['address'] = array_column($data['address'], 'address');

        if ($table == 'patent') {
            // patent才有奖项
            $data['prize_name'] = Db::name('web_honor_patent')->where($where)->group('prize_name')->column('prize_name');
        }


        // $all = [];
        // if($data)
        // {
        //     array_unshift($data, $all);
        // }

        return ajaxReturn('success', 1, $data);
    }


    /**
     * 返回指定数据的年份
     */
    public function get_years()
    {   
        $table = input('table') ?? '';
        $type = input('type') ?? '';
        $address = input('address') ?? '';
        $prize_name = input('prize_name') ?? '';

        if (empty($table)) {
            return ajaxReturn('table不能为空', 0);
        }

        $where = [];

        if ($table == 'examples') {
            if (empty($type)) {
                return ajaxReturn('示范企业类型未选择', 0);
            }

            $where[] = ['type', '=', $type];
        }

        
        if ($address) {
            $where[] = ['address', '=', $address];
        }

        if ($prize_name) {
            $where[] = ['prize_name', '=', $prize_name];
        }

        $data = Db::name('web_honor_'.$table)->where($where)->group('years')->order('years asc')->column('years');


        // $all = [];
        // if($data)
        // {
        //     array_unshift($data, $all);
        // }

        return ajaxReturn('success', 1, $data);
    }


    /**
     * 返回指定数据的街镇
     */
    public function get_address()
    {   
        $table = input('table') ?? '';
        $type = input('type') ?? '';
        $years = input('years') ?? '';
        $prize_name = input('prize_name') ?? '';

        if (empty($table)) {
            return ajaxReturn('table不能为空', 0);
        }

        $where = [];

        if ($table == 'examples') {
            if (empty($type)) {
                return ajaxReturn('示范企业类型未选择', 0);
            }

            $where[] = ['type', '=', $type];
        }

        
        if ($years) {
            $where[] = ['years', '=', $years];
        }

        if ($prize_name) {
            $where[] = ['prize_name', '=', $prize_name];
        }

        $data = Db::name('web_honor_'.$table)->where($where)->group('address')->column('address');

        // $all = [];
        // if($data)
        // {
        //     array_unshift($data, $all);
        // }

        return ajaxReturn('success', 1, $data);
    }


    /**
     * 返回指定数据的奖项名
     */
    public function get_prize_name()
    {   
        $years = input('years') ?? '';
        $address = input('address') ?? '';

        $where = [];
        
        if ($years) {
            $where[] = ['years', '=', $years];
        }

        if ($address) {
            $where[] = ['address', '=', $address];
        }

        $data = Db::name('web_honor_patent')->where($where)->group('prize_name')->column('prize_name');

        // $all = [];
        // if($data)
        // {
        //     array_unshift($data, $all);
        // }

        return ajaxReturn('success', 1, $data);
    }



    /**
     * 荣誉列表
     */ 
    public function prize_list()
    {
        // 搜索
        $prize = input('prize') ?? 0;
        $years = input('years') ?? '';

        $where = [];
        if ($prize) {
            $where[] = ['prize', '=', $prize];
        }

        if ($years) {
            $where[] = ['years', '=', $years];
        }

        $list = [];

        // 金奖
        $list['gold'] = Db::name('web_honor_patent')->where($where)->where('prize', 1)->order('myorder desc')->select()->toArray();
        $list['silver'] = Db::name('web_honor_patent')->where($where)->where('prize', 2)->order('myorder desc')->select()->toArray();
        $list['excellent'] = Db::name('web_honor_patent')->where($where)->where('prize', 3)->order('myorder desc')->select()->toArray();

        return ajaxReturn('success', 1, $list);

    }



    /**
     * 对话顺德详情
     */
    public function dialogue_info()
    {
        $id = input('honor_dialogue_id');
        if (!$id) {
            return ajaxReturn('请求参数出错', 0);
        }

        $data = Db::name('web_honor_dialogue')->find($id);
        if (empty($data)) {
            return ajaxReturn('不存在的对话顺德', 0);
        }

        // 数据处理
        $data['online_time'] = date('Y-m-d', $data['online_time']);
        $data['content'] = htmlspecialchars_decode($data['content']);

        // 上一篇 下一篇
        $data['prev'] = Db::name('web_honor_dialogue')->where('id', '<', $id)->max('id');
        $data['next'] = Db::name('web_honor_dialogue')->where('id', '>', $id)->min('id');

        return ajaxReturn('success', 1, $data);
    }



    /**
     * 之前的版本
     */
    public function list1()
    {
        // 专利奖条数
        $patent_size = !empty(input('patent_size')) ? input('patent_size') : 10;
        // 示范企业展示条数
        $company_size = !empty(input('company_size')) ? input('company_size') : 10;
        // 商标条数
        $trademark_size = !empty(input('trademark_size')) ? input('trademark_size') : 10;
        
        $list = [];
        $list['patent_list'] = Db::name('web_honor_patent')
                                ->order('myorder desc, create_time desc')
                                ->limit($patent_size)
                                ->select()
                                ->toArray();

        foreach ($list['patent_list'] as $key => $value) {
            $list['patent_list'][$key]['company'] = Db::name('web_company')->where('id', $value['company_id'])->value('name');
            $prize = Db::name('web_product_prize')->where('id', $value['prize'])->value('name');

            // 将金银奖拆出来 前端做图片输出判断
            $prize_arr = ['金奖', '银奖', '优秀奖'];
            foreach ($prize_arr as $k => $v) {
                if (strpos($prize, $v) !== false) {
                    $list['patent_list'][$key]['prize'] = explode($v, $prize)[0];
                    $list['patent_list'][$key]['prize_type'] = $v;
                    break;
                }
            }
            
        }


        $list['company_list'] = Db::name('web_honor_examples')
                                ->order('myorder desc')
                                ->limit($patent_size)
                                ->select()
                                ->toArray();

        foreach ($list['company_list'] as $key => $value) {
            $list['company_list'][$key]['name'] = Db::name('web_company')->where('id', $value['company_id'])->value('name'); 
            $list['company_list'][$key]['href'] = Db::name('web_company')->where('id', $value['company_id'])->value('href'); 
            $list['company_list'][$key]['tpic'] = Db::name('web_company')->where('id', $value['company_id'])->value('tpic'); 
            $list['company_list'][$key]['id'] = $value['company_id']; 
        }

        $list['trademark_list'] = Db::name('web_honor_trademark')
                                    ->order('myorder desc, create_time desc')
                                    ->limit($patent_size)
                                    ->select()
                                    ->toArray();      
                                    
        // 总
        $list['patent_num'] = Db::name('web_honor_patent')->count();
        $list['company_num'] = Db::name('web_honor_examples')->count();
        $list['trademark_num'] = Db::name('web_honor_trademark')->count();

        return ajaxReturn('success', 1, $list);
    }



}